package com.hdu.dwh.controller;


import com.hdu.dwh.dao.PaimonTableDao;
import com.hdu.dwh.dtos.PageRequestDto;
import com.hdu.dwh.dtos.SourceAccessDto;
import com.hdu.dwh.pojos.source_access_pojos.SourceAccess;
import com.hdu.dwh.pojos.source_access_pojos.SourceAccessDatabase;
import com.hdu.dwh.pojos.source_access_pojos.SourceAccessDetails;
import com.hdu.dwh.service.ISourceAccessService;
import com.hdu.dwh.utils.TrinoQueryUtils;
import com.hdu.dwh.results.ResponseResult;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.validation.Valid;
import lombok.RequiredArgsConstructor;

import java.util.Set;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

/**
 * <p>
 * 数据接入表 前端控制器
 * </p>
 *
 * @author bobo
 * @since 2025-04-08
 */
@RestController
@RequestMapping("/source/access")
@RequiredArgsConstructor
@Tag(name = "数据接入")
public class SourceAccessController {
    private final ISourceAccessService sourceAccessService;

    @Autowired
    private PaimonTableDao paimonTableDao;

    @Autowired
    private TrinoQueryUtils trinoQueryUtils;

    @Operation(summary ="新建接入")
    @PostMapping("/add")
    public ResponseResult add(@Valid @RequestBody SourceAccessDto sourceAccessDto){
        return sourceAccessService.add(sourceAccessDto);
    }

    @Operation(summary ="编辑接入")
    @PutMapping("/{id}")
    public ResponseResult edit(@RequestBody SourceAccessDto sourceAccessDto, @PathVariable Long id){
        return sourceAccessService.edit(sourceAccessDto, id);
    }

    @Operation(summary ="删除接入")
    @DeleteMapping("/{id}")
    public ResponseResult delete(@PathVariable Long id){
        return sourceAccessService.delete(id);
    }

    @Operation(summary ="查询接入列表（分页）")
    @PostMapping("/page")
    public ResponseResult page(@RequestBody PageRequestDto pageRequestDto){
        return sourceAccessService.pageQuery(pageRequestDto);
    }

    @Operation(summary = "查询接入详情")
    @GetMapping("/{id}")
    public ResponseResult<SourceAccessDetails> getById(@PathVariable Long id) throws Exception{
        SourceAccess sourceAccess = sourceAccessService.getById(id);
        if(sourceAccess == null){
            throw new Exception("接入信息不存在, SourceAccessId = " + id);
        }
        SourceAccessDetails sourceAccessDetails = sourceAccessService.getAll_dbs_tables_columnsFromSourceAccess(sourceAccess);
        return ResponseResult.success(sourceAccessDetails);
    }

    @Operation(summary = "查询paimon详情")
    @GetMapping("/paimon")
    public ResponseResult<Set<SourceAccessDatabase>> getPaimon() throws Exception{
        Set<SourceAccessDatabase> paimonDatabases = sourceAccessService.getAllDatabaseDetails(trinoQueryUtils.createTrinoConnection(), "paimon");
        // 不暴露系统Schema
        paimonDatabases.removeIf(db -> db.getDatabaseName().equals("system") || db.getDatabaseName().equals("information_schema"));
        return ResponseResult.success(paimonDatabases);
    }

    @Operation(summary = "删除paimon表", deprecated = true)
    @DeleteMapping("/paimon/{dbName}/{tableName}")
    public ResponseResult deletePaimonTable(@PathVariable String dbName, @PathVariable String tableName) throws Exception{
        // trinoQueryUtils.deletePaimonTable(trinoQueryUtils.createTrinoConnection(), dbName, tableName);
        return ResponseResult.success();
    }
}
